﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VemTransporte.Models.Fachada;
using VemTransporte.Models.Basica;
using VemTransporte.Models.Factory;
using System.Globalization;

namespace VemTransporte
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListDia.Items.Clear();
                DropDownListDia.Items.Add("Dia");
                DropDownListMes.Items.Clear();
                DropDownListMes.Items.Add("Mês");
                DropDownListAno.Items.Clear();
                DropDownListAno.Items.Add("Ano");

                for (int i = 1; i <= 31; i++)
                {
                    DropDownListDia.Items.Add(string.Format("{0:00}", i));
                }
                CultureInfo culture = new CultureInfo("pt-BR");
                DateTimeFormatInfo dtfi = culture.DateTimeFormat;

                for (int i = 1; i <= 12; i++)
                {
                    string mes = culture.TextInfo.ToTitleCase(dtfi.GetMonthName(i));
                    DropDownListMes.Items.Add(mes);
                }

                for (int i = 1910; i <= 2011; i++)
                {
                    DropDownListAno.Items.Add(Convert.ToString(i));
                }
            }

        }

        protected void CreateUserButton_Click(object sender, EventArgs e)
        {
            try
            {
                Usuario novoUsuario = PessoaFactory.Instance.CreateUsuario();

                novoUsuario.Nome = UserName.Text;
                novoUsuario.Email = Email.Text;
                novoUsuario.Senha = Password.Text;
                novoUsuario.Nascimento = DateTime.Parse(
                    DropDownListDia.Items[DropDownListDia.SelectedIndex].Text + "/" +
                    DropDownListMes.SelectedIndex + "/" +
                        DropDownListAno.Items[DropDownListAno.SelectedIndex].Text);
                Fachada.Instance.Salvar(novoUsuario);

            }
            catch (Exception ex)
            {
                ErrorMessage.Text = ex.Message;
            }
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                Usuario usuarioLogado = Fachada.Instance.ProcurarUsuarioPorEmailESenha(TextBoxEmail.Text, TextBoxSenha.Text);
                if (usuarioLogado != null)
                {
                    //Redirect para o mapa implementar aqui

                }
                else
                {
                    //Usuario não encontrado exception
                    Response.Redirect("/Views/UsuarioTransporte/Login.aspx");
                }
            }
            catch(Exception ex)
            {
                Response.Redirect("/Views/UsuarioTransporte/Login.aspx?ErrorMessageLogin=" + ex.Message);
            }

        }
    }
}